package com.sdk.es.http.gen;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * @author limaosheng
 * @create 2022-05-10 14:18
 * @desc
 */
public class FileUtil {

    public static void write(String path,String data,Boolean isOver){
        try {
            System.out.println("文件地址：" + path);
            File writeName = new File(path); // 相对路径，如果没有则要建立一个新的output.txt文件
            if(writeName.exists()) {
                System.out.println("文件已存在");
                if(isOver){
                    System.out.println("覆盖原文件");
                    writeName.createNewFile();
                }else {
                    System.out.println("不覆盖原文件");
                    return;
                }
            }else {
                System.out.println("文件未存在");
                writeName.createNewFile();
            }
            FileWriter writer = new FileWriter(writeName);
            BufferedWriter out = new BufferedWriter(writer);
            out.write(data);
            out.flush(); // 把缓存区内容压入文件    out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成失败");
        }
    }
    public static void write(String path,String data) {
        write(path,data,false);
    }

    public static void main(String[] args) throws Exception{
        File file = new File("C:\\Users\\87683\\Desktop\\project\\community-elasticsearch\\common\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\entity\\saas\\fuck.java");
        System.out.println(file.exists());
        file.createNewFile();
        System.out.println(file.exists());
    }
}
